package com.yxb.api.rest;


import com.allen.page.DataPage;
import com.yxb.config.rest.RestResult;
import com.yxb.service.common.domain.DeviceData;
import com.yxb.service.common.interf.DeviceDataService;
import com.yxb.service.common.option.DeviceDataOption;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author
 */
@RestController
@RequestMapping("api")
public class DeviceDataController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeviceDataController.class);

    @Resource
    private DeviceDataService deviceDataService;

    @GetMapping("/device/accept")
    public ResponseEntity<RestResult> accept(Integer dId, Integer userId){
        if ( dId == null || dId.intValue() == 0 || userId == null || userId.intValue() ==0 ){
            LOGGER.info("缺少参数");
            return ResponseEntity.ok(RestResult.errorOfParamInvalid("缺少参数"));
        }

        ImmutablePair<Boolean, String>  immutablePair = deviceDataService.userAccept(userId, dId);
        if ( !immutablePair.getLeft() ){
            return ResponseEntity.ok(RestResult.errorOfException(immutablePair.getRight()));
        }
        return ResponseEntity.ok(RestResult.success());
    }


    @GetMapping("/devices/user/list")
    public ResponseEntity<RestResult> listDeviceByUser(Integer userId, Boolean status,
                                                       Integer pageNo, Integer size){

        if ( userId == null || userId.intValue() ==0 ){
            LOGGER.info("userId缺少参数");
            return ResponseEntity.ok(RestResult.errorOfParamInvalid("缺少参数"));
        }

        if ( status == null  ){
            LOGGER.info("status 缺少参数");
            return ResponseEntity.ok(RestResult.errorOfParamInvalid("缺少参数"));
        }

        DeviceDataOption  dataOption = new DeviceDataOption();
        dataOption.setIsAccept(status);

        List<DeviceData> dataList = null;
        if ( pageNo == null && size == null ){
            dataOption.setAcceptUserId(String.valueOf(userId));
            dataList = deviceDataService.queryList(dataOption);
            if ( dataList == null ){
                dataList = new ArrayList<>();
            }
            return ResponseEntity.ok(RestResult.success(dataList));
        }


        if ( pageNo <= 0){
            pageNo = 1;
        }

        if ( size <= 0){
            size = 5;
        }

        DataPage<DeviceData> dataPage = new DataPage<>(pageNo, size);
        dataPage.setOrder("desc");
        dataPage.setOrderBy("create_time");
        dataPage.setNeedTotalCount(false);
        dataOption = new DeviceDataOption();
        dataOption.setIsAccept(status);
        DataPage<DeviceData>  deviceDataDataPage =  deviceDataService.queryPage(dataPage, dataOption, userId);
        dataList = deviceDataDataPage.getDataList();
        if ( dataList.isEmpty() ){
            return ResponseEntity.ok(RestResult.success(new ArrayList<>()));
        }

        return ResponseEntity.ok(RestResult.success(dataList));
    }


    @GetMapping("/devices/list")
    public ResponseEntity<RestResult> listDeviceByDeviceId( String deviceIds, Boolean status,
                                                       Integer pageNo, Integer size){

        if (StringUtils.isBlank(deviceIds)){
            LOGGER.info("deviceId缺少参数");
            return ResponseEntity.ok(RestResult.errorOfParamInvalid("缺少参数"));
        }
        if ( status == null  ){
            LOGGER.info("status 缺少参数");
            return ResponseEntity.ok(RestResult.errorOfParamInvalid("缺少参数"));
        }

        String [] deviceIdArray = deviceIds.split(",");

        List<DeviceData> dataList = null;
        DeviceDataOption  dataOption = new DeviceDataOption();
        dataOption.setIsAccept(status);
        dataOption.setDeviceIdList(Arrays.asList(deviceIdArray));
        if ( pageNo == null && size == null ){
            dataList = deviceDataService.queryList(dataOption);
            if ( dataList == null ){
                dataList = new ArrayList<>();
            }
            return ResponseEntity.ok(RestResult.success(dataList));
        }

        if ( pageNo <= 0){
            pageNo = 1;
        }

        if ( size <= 0){
            size = 5;
        }

        DataPage<DeviceData> dataPage = new DataPage<>(pageNo, size);
        dataPage.setOrder("desc");
        dataPage.setOrderBy("create_time");
        dataPage.setNeedTotalCount(false);
        DataPage<DeviceData>  deviceDataDataPage =  deviceDataService.queryDevicePage(dataPage, dataOption);
        dataList = deviceDataDataPage.getDataList();
        if ( dataList.isEmpty() ){
            return ResponseEntity.ok(RestResult.success(new ArrayList<>()));
        }

        return ResponseEntity.ok(RestResult.success(dataList));
    }









}
